package com.transaction.manage.localMapper;

import com.transaction.manage.model.dto.FlowInfo;
import com.transaction.manage.myMapper.MyMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.LinkedHashMap;
import java.util.List;

public interface FlowInfoMapper extends MyMapper<FlowInfo> {

    @Select("SELECT step_code stepCode, step_name stepName FROM flow_info t WHERE t.flow_code = #{flowCode} ORDER BY t.step_code ")
    List<FlowInfo> selectFlowStepCodeNameByFlowCode(@Param("flowCode") String flowCode);

    List<FlowInfo> selectFlowInfoList(FlowInfo flowInfo);

    @Select("SELECT * FROM flow_info f WHERE f.flow_code = #{flowCode} AND f.step_code = #{stepCode} ")
    FlowInfo selectFlowInfoByFlowCodeAndStepCode(@Param("flowCode") String flowCode, @Param("stepCode") Integer stepCode);

    void updateFlowInfoByFlowCodeAndStepCode(FlowInfo flowInfo);

    void insertFlowInfo(FlowInfo flowInfo);

    void updateFlowInfo(FlowInfo flowInfo);

    @Update("update ${tableName} set flow_step = concat(flow_step,#{flowStep}) where channel_code = #{channelCode} AND handle_date = #{handleDate}")
    void asyncUpdateFlowStep(@Param("tableName") String tableName, @Param("flowStep") String flowStep,
                             @Param("channelCode") String channelCode,@Param("handleDate") String handleDate);

    @Delete("DELETE from ${tableName} WHERE channel_code = #{channelCode} AND handle_date = #{handleDate} ")
    void deleteAnalysisData(@Param("tableName") String tableName,
                            @Param("channelCode") String channelCode,
                            @Param("handleDate") String handleDate);

    void updateStatusAndMsgByAppSheetSerialNo(@Param("tableName") String tableName,
                                              @Param("channelCode") String channelCode,
                                              @Param("handleDate") String handleDate,
                                              @Param("sameAppSheetSerialNo") List<String> sameAppSheetSerialNo,
                                              @Param("errorMsg") String errorMsg);

    void updateSerialNoAndCheckErrorList(@Param("tableName") String tableName,
                                         @Param("dataList") List<LinkedHashMap<String, Object>> dataList);

    @Update("UPDATE ${tableName} SET check_status = 1 ,error_msg = '' WHERE channel_code = #{channelCode} AND handle_date = #{handleDate}")
    void initCheckStatusAndErrorMsg(@Param("tableName") String tableName,
                                    @Param("channelCode") String channelCode,
                                    @Param("handleDate") String handleDate);

    @Update("UPDATE ${tableName} SET check_status = 1 WHERE channel_code = #{channelCode} AND handle_date = #{handleDate} " +
            "AND RIGHT(flow_step, 18) = #{key} AND check_status = 0 ")
    void recoverFailedData(@Param("tableName") String tableName,
                           @Param("channelCode") String channelCode,
                           @Param("handleDate") String handleDate,
                           @Param("key") String key);
}